class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        int[] in=new int[numCourses];
        Map<Integer,List<Integer>> edges = new HashMap<>();
        for(int i=0;i<prerequisites.length;i++){
            int a=prerequisites[i][0];
            int b=prerequisites[i][1];
            if(!edges.containsKey(b)){
                edges.put(b,new ArrayList<Integer>());
            }
            edges.get(b).add(a);
            in[a]++;
        }
        Queue<Integer> q=new LinkedList<>();
        for(int i=0;i<numCourses;i++){
            if(in[i]==0)
                q.add(i);
        }
        while(!q.isEmpty()){
            int t=q.poll();
            for(int x :edges.getOrDefault(t,new ArrayList<>())){
                in[x]--;
                if(in[x]==0) q.add(x);
            }
        }
        for(int a:in){
            if(a!=0) return false;
        }
        return true;
    }
}